<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class ome_ctl_admin_expressfee extends desktop_controller{
    
    var $workground = 'ome_ctl_admin_expressfee';
    var $pagelimit = 10;
    var $expressfee_model ;
    public function __construct($app)
    {
        parent::__construct($app);
        $this->expressfee_model = $this->app->model('expressfee');
        header("cache-control: no-store, no-cache, must-revalidate");
    }
   function index(){
   	
        $custom_actions[] = array('label'=>app::get('ome')->_('添加'),'href'=>'index.php?app=ome&ctl=admin_expressfee&act=add_expressfee','target'=>'_blank');


        $actions_base['title'] = app::get('ome')->_('应收快递费配置');
        $actions_base['actions'] = $custom_actions;
        $actions_base['allow_detail_popup'] = true;
        $actions_base['use_buildin_set_tag'] = false;
        $actions_base['use_buildin_export'] = false;
        $actions_base['use_buildin_filter'] = false;
        $actions_base['use_buildin_recycle'] = true;

        $actions_base['use_view_tab'] = false;

        $this->finder('ome_mdl_expressfee',$actions_base);
    }
    //保存、编辑校验时间逻辑
    function validate($sdf)
    {
        if(empty($sdf['effective_time']))
        {
            $this->end(false, app::get('base')->_('请选择生效时间！'));
            exit;
        }
        if(empty($sdf['disable_time']))
        {
            $this->end(false, app::get('base')->_('请选择失效时间！'));
            exit;
        }
        if(strtotime($sdf['effective_time'])>strtotime($sdf['disable_time']))
        {
            $this->end(false, app::get('base')->_('生效时间大于失效时间，请重新选择！'));
            exit;   
        }
    }
    //保存应收快递费
    function saveExpressfee()
    {
        $this->begin('index.php?app=ome&ctl=admin_expressfee&act=index');
        $dlycorp = $_POST;
        $this->validate($_POST);

        $sql = "SELECT distinct AXCustomer,customername from sdb_ome_shop where shopstatus='1' and AXCustomer='".$_POST['shop']."'";
        $shopMdl_datas = kernel::database()->select($sql);
        $expressfee_model = $this->app->model('expressfee');
        $expressfee_objects_model = $this->app->model('expressfee_objects');
        $expressfee_id='';
        $arr_order = '';
        
        $arr_order['id'] = $_POST['id'];
        $arr_order['shop_code'] = $shopMdl_datas[0]['AXCustomer'];
        $arr_order['shop_name'] = $shopMdl_datas[0]['customername'];
        $arr_order['JSType'] = $_POST['JSType'];
        $arr_order['effective_time'] = strtotime($_POST['effective_time']);
        $arr_order['disable_time'] =strtotime($_POST['disable_time']);
        $arr_order['remark'] = '';

            //保存快递费配置
        if($_POST['id']>0)
        {
            $expressfee_id=$_POST['id']; 
            if($expressfee_id>0)
            {
                $sql="delete from sdb_ome_expressfee_objects where expressfee_id=".$expressfee_id;
                kernel::database()->exec($sql);
                //$expressfee_objects_model->delete(array('id' => $expressfee_id));
            }
            $expressfee_model->update($arr_order,array('id' => $_POST['id'] ));
        }else{
            $expressfee_model->save($arr_order);
            $expressfee_id=$arr_order['id'];
        }
        $area_fee_conf = array();
        if ($dlycorp['area_fee_conf']['areaGroupName'])
        {
            foreach ($dlycorp['area_fee_conf']['areaGroupName'] as $k => $v) 
            {
                $ishave = 0;
                $c['areaGroupName'] = $v;
                //物流公司特殊处理
                $logi= explode(",", $dlycorp['area_fee_conf']['logi'][$k]);
                $logistics[]='';
                foreach ($logi as $key => $value) 
                {   
                    if(!empty($value))
                    {
                        if (!in_array($value, $logistics))
                        {
                            $logistics[]=$value; 
                        }
                    }
                }
                $c['logistics'] =implode(",", $logistics) ;
                unset($logistics);
                $c['areaGroupId'] = $dlycorp['area_fee_conf']['areaGroupId'][$k];
                $c['firstprice'] = $dlycorp['area_fee_conf']['firstprice'][$k];
                $c['continueprice'] = $dlycorp['area_fee_conf']['continueprice'][$k];
                $c['dt_expressions'] = $dlycorp['area_fee_conf']['dt_expressions'][$k];
                $c['dt_useexp'] = $dlycorp['area_fee_conf']['dt_useexp'][$k];
                #增加首重续重
                $c['firstunit'] = $dlycorp['area_fee_conf']['firstunit'][$k];
                $c['continueunit'] = $dlycorp['area_fee_conf']['continueunit'][$k];
                if($c['firstunit'] <= 0 || $c['continueunit'] <= 0){
                    $this->end(false, app::get('base')->_('首重和续重必须设置并大于0'));
                }
                #
                if ($c['firstprice'] < 0 || $c['continueprice'] < 0) {
                    $this->end(false, app::get('base')->_('配送地区的首重费用和续重费用都不能为负！'));
                }

                if (!$c['areaGroupName'] and !$c['areaGroupId'] and !$c['firstprice'] and !$c['continueprice'] and !$c['dt_expressions'] and !$c['dt_useexp'])
                    $ishave = 0;
                else
                    $ishave = 1;
                $area_fee_conf[] = $c;
            }

            foreach ($area_fee_conf as $key => $value) 
            {
                $areaGroupId=str_replace("|", ",", $value['areaGroupId']);
                $areaGroupId=str_replace("close", "", $areaGroupId);
                $areaGroupId= explode(",", $areaGroupId);
                foreach ($areaGroupId as $k => $v) 
                {
                    if(empty($v))
                        continue;
                    $arr_objects = '';
                    $arr_objects['objects']['expressfee_id'] = $expressfee_id;
                    $arr_objects['objects']['logistics'] = $value['logistics'];
                    $arr_objects['objects']['region_id'] = $v;
                    $arr_objects['objects']['firstunit'] =  $value['firstunit'];
                    $arr_objects['objects']['continueunit'] =$value['continueunit'];
                    $arr_objects['objects']['firstprice'] = $value['firstprice'];
                    $arr_objects['objects']['continueprice'] = $value['continueprice'];
                    $arr_objects['objects']['dt_expressions'] = $value['dt_expressions'];
                    $arr_objects['objects']['dt_useexp'] = $value['dt_useexp'];
                    $arr_objects['objects']['areaGroupId'] = $value['areaGroupId'];
                    $arr_objects['objects']['areaGroupName'] = $value['areaGroupName'];
                    
                    $expressfee_objects_model->insert($arr_objects['objects']);
                }

            }
        }
        $this->end(true, app::get('base')->_('保存成功'));
    }
    //编辑运费
    function editexpressfee()
    {
         #获取当前仓库模式
        $id=$_GET['id'];

        $expressfee_model = $this->app->model('expressfee');     
        $mdl_dly_corp = app::get('ome')->model('dly_corp');//物流公司   
        $expressfee_model_data=$expressfee_model->getList('*', array( 'id'=> $id));
        $dly_corp_data=$mdl_dly_corp->getList('*', array('disabled' => 'false', 'd_type'=> '1'));
        $this->pagedata['expressfee_model'] = $expressfee_model_data[0];
        $this->pagedata['expressfee_model']['area_fee_conf']= $expressfee_model->getAreaConfig($id);
        $this->pagedata['logi_dly_corp'] = $dly_corp_data;       

        $sql = "SELECT distinct AXCustomer,customername from sdb_ome_shop where shopstatus='1' and AXCustomer!='';";
        $shopMdl_datas = kernel::database()->select($sql);
        $this->pagedata['shop_data'] = $shopMdl_datas;

        $this->pagedata['title'] = '编辑应收快递费';
        $this->singlepage('admin/expressfee/add_expressfee.html');
    } 
    //新建类似
    function addeditexpressfee()
    {
         #获取当前仓库模式
        $id=$_GET['id'];

        $expressfee_model = $this->app->model('expressfee');     
        $mdl_dly_corp = app::get('ome')->model('dly_corp');//物流公司   
        $expressfee_model_data=$expressfee_model->getList('*', array( 'id'=> $id));
        $dly_corp_data=$mdl_dly_corp->getList('*', array('disabled' => 'false', 'd_type'=> '1'));
        unset($expressfee_model_data[0]['id']);
        $this->pagedata['expressfee_model'] = $expressfee_model_data[0];
        $this->pagedata['expressfee_model']['area_fee_conf']= $expressfee_model->getAreaConfig($id);
        $this->pagedata['logi_dly_corp'] = $dly_corp_data;       

        $sql = "SELECT distinct AXCustomer,customername from sdb_ome_shop where shopstatus='1' and AXCustomer!='' order by AXCustomer asc;";
        $shopMdl_datas = kernel::database()->select($sql);
        $this->pagedata['shop_data'] = $shopMdl_datas;

        $this->pagedata['title'] = '新建应收快递费';
        $this->singlepage('admin/expressfee/add_expressfee.html');
    }
    /**
     * 设置默认仓库
     * 
     * @param Integer $bid 仓库ID
     * @return void 
     */
    function setApprove($id) 
    {
        $sql="update sdb_ome_expressfee set status='1',op_id='".kernel::single ( 'desktop_user' )->get_id ()."',approve_time=".time()."  where id=".$id;
        kernel::database()->exec($sql);
        
        echo "<script>alert('审核成功！！！');top.finderGroup['{$_REQUEST[finder_id]}'].refresh();</script>";
    }   
    function setunApprove($id) 
    {
        $sql="update sdb_ome_expressfee set status='0',op_id=null,approve_time=null  where id=".$id;
        kernel::database()->exec($sql);
        
        echo "<script>alert('弃审成功！！！');top.finderGroup['{$_REQUEST[finder_id]}'].refresh();</script>";
    }
    function add_expressfee()
    {
         #获取当前仓库模式
        $branch_mode = $this->app->getConf('ome.branch.mode');

        $this->pagedata['config'] = array(
            'firstunit' => '1000',
            'continueunit' => '1000'
        );
        $sql = "SELECT distinct AXCustomer,customername from sdb_ome_shop where shopstatus='1' and AXCustomer!=''  order by AXCustomer asc;";
        $shopMdl_datas = kernel::database()->select($sql);
        $this->pagedata['shop_data'] = $shopMdl_datas;

        $mdl_dly_corp = app::get('ome')->model('dly_corp');//物流公司
        $dly_corp_data=$mdl_dly_corp->getList('*', array('disabled' => 'false', 'd_type'=> '1'));

        $this->pagedata['logi_dly_corp'] = $dly_corp_data;
  

        $this->pagedata['weightunit'] = $this->_weightunit();
        $this->pagedata['branch_mode'] = $branch_mode;
        unset($branch_mode);

        $this->pagedata['title'] = '添加应收快递费配置';
        $this->singlepage('admin/expressfee/add_expressfee.html');
    }

    function _weightunit() {
        return array(
            "500" => app::get('base')->_("500克"),
            "1000" => app::get('base')->_("1公斤"),
            "1200" => app::get('base')->_("1.2公斤"),
            "2000" => app::get('base')->_("2公斤"),
            "5000" => app::get('base')->_("5公斤"),
            "10000" => app::get('base')->_("10公斤"),
            "20000" => app::get('base')->_("20公斤"),
            "50000" => app::get('base')->_("50公斤")
        );
    }

}
